package com.cookor.poi.modules.phy.web;

import com.cookor.poi.common.config.Global;
import com.cookor.poi.common.persistence.Page;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.common.web.BaseController;
import com.cookor.poi.modules.phy.entity.TDoctor;
import com.cookor.poi.modules.phy.entity.TSchedueTemplate;
import com.cookor.poi.modules.phy.service.TDoctorService;
import com.cookor.poi.modules.phy.service.TSchedueTemplateService;
import com.cookor.poi.modules.sys.utils.UserUtils;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping({"${adminPath}/phy/tSchedueTemplate"})
public class TSchedueTemplateController extends BaseController {
  @Autowired
  private TSchedueTemplateService tSchedueTemplateService;
  
  @Autowired
  private TDoctorService tDoctorService;
  
  @ModelAttribute
  public TSchedueTemplate get(@RequestParam(required = false) String id) {
    TSchedueTemplate entity = null;
    if (StringUtils.isNotBlank(id))
      entity = this.tSchedueTemplateService.get(id); 
    if (entity == null)
      entity = new TSchedueTemplate(); 
    return entity;
  }
  
  @RequiresPermissions({"phy:tSchedueTemplate:view"})
  @RequestMapping({"list", ""})
  public String list(TSchedueTemplate tSchedueTemplate, HttpServletRequest request, HttpServletResponse response, Model model) {
    tSchedueTemplate.setTenantId(UserUtils.getTenantId());
    Page<TSchedueTemplate> page = this.tSchedueTemplateService.findPage(new Page(request, response), tSchedueTemplate);
    model.addAttribute("page", page);
    model.addAttribute("tSchedueTemplate", tSchedueTemplate);
    return "modules/phy/tSchedueTemplateList";
  }
  
  @RequiresPermissions({"phy:tSchedueTemplate:view"})
  @RequestMapping({"form"})
  public String form(TSchedueTemplate tSchedueTemplate, Model model) {
    model.addAttribute("admin", UserUtils.getUser());
    model.addAttribute("tSchedueTemplate", tSchedueTemplate);
    return "modules/phy/tSchedueTemplateForm";
  }
  
  @RequiresPermissions({"phy:tSchedueTemplate:edit"})
  @RequestMapping({"save"})
  public String save(TSchedueTemplate tSchedueTemplate, Model model, RedirectAttributes redirectAttributes) throws Exception {
    if (StringUtils.isBlank(tSchedueTemplate.getId()) && tSchedueTemplate.getDoctorId() != null) {
      TDoctor tDoctor = this.tDoctorService.get(tSchedueTemplate.getDoctorId().toString());
      tSchedueTemplate.setOfficeId(tDoctor.getOfficeId());
      if (tDoctor.getBookStatus().intValue() == 0)
        throw new Exception("该医生未开启预约挂号功能，不能设置排班模板"); 
    } 
    if (!beanValidator(model, tSchedueTemplate, new Class[0]))
      return form(tSchedueTemplate, model); 
    if (StringUtils.isBlank(tSchedueTemplate.getId())) {
      TSchedueTemplate query = new TSchedueTemplate();
      query.setDoctorId(tSchedueTemplate.getDoctorId());
      query.setTenantId(UserUtils.getTenantId());
      query.setDelFlag("0");
      List<TSchedueTemplate> list = this.tSchedueTemplateService.findList(query);
      if (!list.isEmpty())
        throw new Exception("该医生在" + ((TSchedueTemplate)list.get(0)).getTenant().getTenantName() + "已存在排班模板"); 
    } 
    this.tSchedueTemplateService.mySave(tSchedueTemplate);
    addMessage(redirectAttributes, new String[] { "保存排班模板成功" });
    return "redirect:" + Global.getAdminPath() + "/phy/tSchedueTemplate/?repage";
  }
  
  @RequiresPermissions({"phy:tSchedueTemplate:edit"})
  @RequestMapping({"delete"})
  public String delete(TSchedueTemplate tSchedueTemplate, RedirectAttributes redirectAttributes) {
    this.tSchedueTemplateService.delete(tSchedueTemplate);
    addMessage(redirectAttributes, new String[] { "删除排班模板成功" });
    return "redirect:" + Global.getAdminPath() + "/phy/tSchedueTemplate/?repage";
  }
}
